<template>
  <div class="topbar" :style="{width: topWidth}">
    <div class="menu">
      <div class="left" style="display: flex">
        <el-icon @click="visibleMenu" v-if="menuOpen && usersetting.menuButton"><fold /></el-icon>
        <el-icon @click="visibleMenu" v-if="!menuOpen && usersetting.menuButton"><expand /></el-icon>
        <!-- <Breadcrumb v-if="setting.showCrumbs"></Breadcrumb> -->
      </div>
      <div class="right">
          <div class="screen" @click="setFullScreen" v-if="!isFullScreen">
              <el-icon><full-screen /></el-icon>
          </div>
          <!-- 缩小屏幕 -->
          <div class="screen" @click="setExitScreen" v-else>
              <el-icon><zoom-out /></el-icon>
          </div>
          <div class="user">
              <el-dropdown>
                  <!-- 头像 姓名 -->
                  <div class="user">
                      <img class="cover" :src="userInfo.circleUrl" style="float: left">
                      <span class="name">{{userInfo.username}}</span>
                  </div>
                  <template #dropdown>
                  <!-- 下拉选项 -->
                    <el-dropdown-menu>
                        <el-dropdown-item>
                            <i class="el-icon-user"></i>
                            <span class="menu-txt" @click="goToUser">个人中心</span>
                        </el-dropdown-item>
                        <el-dropdown-item>
                            <i class="el-icon-switch-button"></i>
                            <span class="menu-txt" @click="loginOut">退出登录</span>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
              </el-dropdown>
          </div>
          <div class="setting" @click="openSetting">
              <el-icon><tools /></el-icon>
          </div>
      </div>
    </div>
    <!-- 标签插槽 -->
    <slot></slot>
  </div>
</template>

<script>
import { getAdmInfo } from "#/User/admin"
import { fullScreen, exitScreen } from "../../utils/utils"
import { useAppStore } from "@/storePinia/index";
import { reactive, toRefs, computed } from 'vue'
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'
  export default {
    name: 'TopBar',
    props: {
      menuOpen: Boolean
    },
    setup (props, context) {
      const router = useRouter();
      const state = reactive({
        // 是否全屏
          isFullScreen: false,
          // 个性设置弹出框
          drawer: false,
          // 用户信息
          userInfo: {
              // 头像
              circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
              // 姓名
              username: '超级管理员'
          },
          menuOpen: props.menuOpen
      })
      let usersetting = computed(() => useAppStore().setting)
      let topWidth = computed(() => {
        return props.menuOpen ? `calc(100% - 250px)` : `calc(100% - 70px)`
      });
      // 初始化个性设置
      const initSetting = () => {
        getAdmInfo(window.sessionStorage.getItem('userId')).then(res => {
          if(res.data.ret === 0) {
            state.userInfo.username = res.data.admUser.name
            state.userInfo.circleUrl = res.data.admUser.headImage
          } else {
            ElMessage.error('获取管理员信息错误')
          }
        })
      }
      // 前往个人中心
      const goToUser = () => {
        router.push('/UserManagement/user')
      }
      // 退出登录
      const loginOut = () => {
        sessionStorage.removeItem("worktab");
        sessionStorage.removeItem("token");
        sessionStorage.removeItem("userId");
        sessionStorage.removeItem("permis");
        useAppStore().resetMenuList();
        router.push('/login');
        // this.$message.info("您已退出登录");
      }
      // 全屏
      const setFullScreen = () => {
        fullScreen();
        state.isFullScreen = true
      }
      // 退出全屏
      const setExitScreen = () => {
        exitScreen();
        state.isFullScreen = false
      }
      // 左侧菜单展开|缩小
      const visibleMenu = () => {
        context.emit('visibleMenu')
      }
      // 打开个性化设置
      const openSetting = () => {
        context.emit('personalityShow')
      }
      initSetting()
      return {
        ...toRefs(state),
        usersetting,
        topWidth,
        initSetting,
        goToUser,
        loginOut,
        setFullScreen,
        setExitScreen,
        visibleMenu,
        openSetting
      }
    },
    // mounted() {
    //   this.initSetting()
    //   document.addEventListener("click", this.bodyCloseNotice)
    // },
    // beforeDestroy() {
    //   document.removeEventListener("click", this.bodyCloseNotice)
    // },
  }
</script>

<style lang="scss" scoped>
  $hover-color: #f8f8f8;

  .topbar {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 1000;
    transition: all .3s ease-in-out;
    // background: $default-background;
    background-color: #F6F7F9;
    background: transparent !important;

    .menu {
      height: 60px;
      line-height: 60px;
      // background: $theme-menu-color;
      background-color: #fff;
      box-shadow: 0 1px 4px rgba(0,21,41,.08);
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      user-select: none;
      -moz-user-select: none;
      -webkit-user-select: none;

      .left {
        line-height: 60px;

        i {
          display: inline-block;
          width: 50px;
          height: 60px;
          line-height: 60px;
          text-align: center;
          cursor: pointer;
          transition: all .2s;
          font-size: 16px;

          &:hover {
            background: $hover-color;
          }
        }

        .el-route {
          line-height: 60px;
          margin-left: 10px;
        }
      }

      .right {
        display: flex;

        .screen {
          cursor: pointer;
          padding: 5px 15px;
          transition: background-color .3s;

          i {
            color: #777;
            font-size: 20px;
          }

          &:hover {
            background: $hover-color;
          }
        }

        .notice {
          cursor: pointer;
          padding: 0 15px;
          transition: background-color .3s;
          position: relative;

          i {
            color: #777;
            font-size: 17px;
          }

          .count {
            display: block;
            width: 6px;
            height: 6px;
            background: red;
            border-radius: 50%;
            position: absolute;
            top: 20px;
            right: 15px;
          }

          &:hover {
            background: $hover-color;
          }
        }

        .user {
          height: 60px;
          line-height: 60px;
          display: flex;
          padding: 0 10px;
          transition: background-color .3s;

          &:hover {
            background: $hover-color;
          }

          &:hover ul {
            height: 80px;
          }

          .cover {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background: #eee;
            margin: 15px 10px 0 0;
            overflow: hidden;
            cursor: pointer;
          }

          .name {
            font-size: 13px;
            cursor: pointer;
          }
        }

        .setting {
          width: 45px;
          height: 65px;
          line-height: 65px;
          text-align: center;
          cursor: pointer;
          transition: background-color .3s;
          
          i {
            font-size: 16px;
          }

          &:hover {
            background: #f8f8f8;
          }
        }
      }
    }
  }
</style>